<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script src="./lib/vue.js"></script>
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./lib/vue.js"></script>
</head>

<body>
  <div id="app">
    <h3 @click="fn">{{username}}</h3>

  </div>

  <script>

    async function request(url) {
      let ret = await (await fetch(url)).json()
      return ret
    }

    // request('/data/user.json')


    /* let obj1 = { id: 1, fn() { console.log('fn') } }
    let obj2 = { name: '张三' }
    let obj = Object.assign(obj1, obj2)
    console.log(obj === obj1) */

    // 全局混入
    Vue.mixin({
      // 混入data数据时，一定要用函数返回对象形式
      data() {
        // data数据混入，生效是以离组件最近的数据源为主
        return {
          username: '张三'
        }
      },
      methods: {
        // 优先以组件自身的methods方法为主，如果没有则使用混入 (局部 > 全局)
        fn() {
          console.log('fn -- 全局')
        }
      },
      // 生命周期方法，执行顺序 全局 > 局部 > 组件内
      created() {
        console.log('created - 全局');

        // this.test = '测试'
        this.$http = request
      }
    })

    const mixin = {
      methods: {
        fn() {
          console.log('fn - 局部')
        }
      },
      created() {
        console.log('created - 局部')
      }
    }

    let vm = new Vue({
      el: '#app',
      data: {
        username: 'hello'
      },
      // 局部混入
      mixins: [mixin],
      async created() {
        // console.log(this.test)
        let ret = await this.$http('/data/user.json')
        console.log(ret)

        console.log('created - 组件内')
      },
      methods: {
        /* fn() {
          console.log('fn - 组件内')
        } */
      }
    })
  </script>
</body>

</html>